/**
 * @description 公司公告
 * @author LT
 * @date 2021-07-23
 */
import React, { PureComponent } from 'react';
import { RightOutlined } from '@ant-design/icons';
import { Spin } from 'antd';
import { connect, routerRedux } from 'dva';
import moment from 'moment';
import NoData from '../InfoWarn/component/NoData';
import styles from './index.less';
import titleBg from '../../../../assets/busi/OperatePlatform/title_bg.png';
@connect(({ OperatePlatformM, loading }) => ({
  OperatePlatformM,
  loading: loading.effects['OperatePlatformM/queryCompanyNotice'],
}))
class CompanyNotice extends PureComponent {
  constructor() {
    super();
    this.state = {
      bodyHeight: '',
    };
  }
  // 公司公告
  // queryCompanyNotice
  componentDidMount() {
    this.resize();
    window.addEventListener('resize', this.resize);
    this.props.dispatch({
      type: 'OperatePlatformM/queryCompanyNotice',
      payload: {
        analysisType: '2',
        currentPage: 1,
        numInPage: 10,
      },
    });
  }
  UNSAFE_componentWillReceiveProps() {
    this.resize();
  }
  resize = () => {
    const showHeader = this.props.OperatePlatformM.orgId;
    let heightVal = document.body.clientHeight - 656;
    if (showHeader) {
      heightVal = document.body.clientHeight - 707;
    }
    if (heightVal > 0) {
      this.setState({
        bodyHeight: heightVal,
      });
    } else {
      this.setState({
        bodyHeight: 230,
      });
    }
  };
  // 跳转到公司公告
  handle = () => {
    const { dispatch } = this.props;
    sessionStorage.setItem('riskNews', JSON.stringify({ type: 'notices', analysisType: '2' }));
    dispatch(routerRedux.push('/marketAnalysis/Monitorpublicopinion'));
  };
  // 跳转到详情
  jumpDetail = (val) => {
    let params = {
      bondID: val?.id,
      type: 'notice',
      comid: val?.comcode,
    };
    sessionStorage.setItem('publicSentiment', JSON.stringify(params));
    sessionStorage.setItem('noticeInfo', JSON.stringify(val));
    this.props.dispatch(routerRedux.push(`/marketAnalysis/Monitorpublicopinion/noticeDetail`));
  };
  // 公司公告列表
  renderList = () => {
    const {
      OperatePlatformM: { companyNoticeList },
    } = this.props;
    return companyNoticeList.length === 0 ? (
      <div style={{ height: '100%', display: 'flex', alignItems: 'center', minHeight: '230px' }}>
        <NoData title='暂无数据' />
      </div>
    ) : (
      <div className={styles.listCont}>
        {companyNoticeList.map((item) => {
          return (
            <div
              className={styles.listItem}
              key={item.id}
              onClick={(e) => {
                e.preventDefault();
                this.jumpDetail(item);
              }}
            >
              <div className={styles.itemTitle} title={item.noticeTitle}>
                {item.noticeTitle}
              </div>
              <div className={styles.infoLevel}>
                <span className={styles.time}>{item.noticeDate}</span>
              </div>
            </div>
          );
        })}
      </div>
    );
  };
  render() {
    const { loading } = this.props;
    return (
      <div className={styles.comContainer}>
        <div className={styles.topTitle} style={{ backgroundImage: `url(${titleBg})` }}>
          <span className={styles.title}>公司公告</span>
          <a className={styles.pathLink} onClick={this.handle}>
            更多
            <RightOutlined />
          </a>
        </div>
        <Spin spinning={loading}>
          <div className={styles.listBox} style={{ height: this.state.bodyHeight + 'px', minHeight: '230px' }}>
            {this.renderList()}
          </div>
        </Spin>
      </div>
    );
  }
}
export default CompanyNotice;
